package com.zhentao.controller;

import cn.binarywang.wx.miniapp.bean.WxMaUserInfo;
import com.alibaba.fastjson.JSON;
import com.zhentao.common.dtos.ResponseResult;
import com.zhentao.service.AppUserMemberService;
import com.zhentao.service.AppUserService;
import com.zhentao.service.UserLoginService;
import com.zhentao.user.domain.AppUser;
import com.zhentao.user.dto.UserDto;
import com.zhentao.user.dto.UserInfoDto;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.UUID;

@Api("/zpi/wx/user")
@RestController
@RequestMapping("/zpi/wx/user")
@Slf4j
public class ApUserController {

    @Autowired
    private AppUserService appUserService;

    @Autowired
    private UserLoginService userLoginService;

    @Resource
    private RedisTemplate redisTemplate;

    /**
     * 授权登录
     * @param dto
     * @return
     */
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", dataType = "string", name = "phoneCode", value = "", required = true),
            @ApiImplicitParam(paramType = "query", dataType = "string", name = "loginCode", value = "", required = true)
    })
    @ApiOperation(value = "授权登录", notes = "授权登录", httpMethod = "GET")
    @PostMapping("/miniAppLogin")
    public ResponseResult miniAppLogin(@RequestBody UserDto dto){
        return userLoginService.miniApplogin(dto);
    }

    @GetMapping("test")
    public String  test(){
        String s = UUID.randomUUID().toString();
        AppUser user=new AppUser();
        user.setOpenId(s);
        user.setNickname("假登录");
        String jsonString = JSON.toJSONString(user);
        redisTemplate.opsForValue().set("login:"+user.getOpenId(), jsonString);
        return s;
    }

    /**
     * 首页用户数据
     * @return
     */
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "body", dataType = "UserInfoDto", name = "dto", value = "", required = true)
    })
    @ApiOperation(value = "首页用户数据", notes = "首页用户数据", httpMethod = "GET")
    @PostMapping("/index/home/list")
    public ResponseResult index(@RequestBody UserInfoDto dto){
        return appUserService.index(dto);
    }





}
